home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 203_01 / yam7.c < prev    next >
Text File  |  1980-01-01  |  39KB  |  1,623 lines

  1. /*
  2. $title ('yam7.c: file i/o, initialization')
  3. $date (11 NOV 85)
  4. */
  5. /*
  6.  * File open and close stuff and more ...
  7.  * This file has been modified for operation on a MS-DOS system
  8.  */
  9.  
  10. #include <dos.h>
  11. #include "yam.h"
  12. #include <fcntl.h>
  13.  
  14. /* globals used by dodir */
  15. unsigned Numfiles;  /* Total number of files expanded */
  16. unsigned Blocks;   /* Number of records */
  17.  
  18. /* following are MSDOS system calls */
  19. #define COMPFILSIZ 0x23    /* compute file size */
  20. #define CHANGEDIR 0x3B    /* change current directory */
  21. #define FINDFIRST 0x4E    /* bdos search for file pattern*/
  22. #define FINDNEXT 0x4F    /* search for next occurrence */
  23. #define GETDEFDISK 0x19    /* get current disk */
  24. #define GETFREE 0x36    /* get free disk space */
  25. #define SELDSK 0x0E        /* bdos select disk 0=default disk */
  26. #define SETDMA 0x1A        /* set address for read, write, etc. */
  27. #define DIRAT 0x10        /* directory attribute for file searches */
  28. #define ARCHIVE 0x20    /* archive bit */
  29. #define NOTFOUND 18        /* file not found in search */
  30. #define CHATTRIB 0x43    /* change attribute */
  31. #define CARRYFLG 0x01
  32.  
  33. extern char *getenv();
  34.  
  35. /****************************************************************************
  36. FUNCTION:
  37.     get an single char from console.  Display it as entered.
  38.  
  39. CALLING PARAMETERS:
  40.     none
  41.  
  42. RETURNED PARAMETERS:
  43.     character value typed on console
  44. ===========================================================================*/
  45. char getopt()
  46. {
  47.     char c;
  48.  
  49.         /* note: if <ctype.h> included, tolower lattice c function makes
  50.            this routine request double entrys for each char. */
  51.     c = tolower(getcty());
  52.     putcty(c);
  53.     return c;
  54. } /* getopt */
  55.  
  56.  
  57. /****************************************************************************
  58. FUNCTION:
  59.     open a file for receive.  This file will be written to.
  60.  
  61. CALLING PARAMETERS:
  62.     pathname:
  63.         pointer to ascii file pathname representation
  64. ===========================================================================*/
  65. openrx(pathname)
  66. char *pathname;
  67. {
  68.     char option;
  69. #ifdef RESTRICTED
  70.     char *s;
  71.  
  72.     /* no garbage names please */
  73.     for(s=pathname; *s; )
  74.         if(*s++ > 'z')
  75.             return ERROR;
  76. #endif
  77.     unspace(pathname);
  78.         /* show the name right away */
  79.     printf("'%s' ", pathname);
  80.  
  81.         /* open file for read.  If exists, allow option to overwrite */
  82.     if(!Creamfile && ((fout=fopen(pathname,"r")) != NULL))
  83.     {
  84.             /* close file if successful open */
  85.         fclose(fout);
  86. #ifdef XMODEM
  87.         printf("Can't upload: file exists\n");
  88.         return ERROR;
  89. #else
  90.         printf("Exists, Replace/Append/Quit (r/a/q)?");
  91.         option=getopt();
  92.         if(!index(option, "ary"))
  93.             return ERROR;
  94. #endif
  95.     }
  96.     if(option=='a')
  97.         fout=fopen(pathname,"ab+");
  98.     else
  99.         fout=fopen(pathname,"wb");
  100.     if (fout==NULL)
  101.     {
  102.         printf(" Can't create\n");
  103.         return ERROR;
  104.     }
  105.         /* receive file is open */
  106.     Rfile= TRUE;
  107.     strcpy(Rname, pathname);
  108.  
  109.         /* Dumping means term input will go to disk.  Squelch allows this
  110.            to be turned on and off.  Why I don't know. */
  111.     Dumping = !Squelch;
  112.     printf(" Open\n");
  113. #ifdef STATLINE
  114.     lpstat("Receiving %s", Rname);
  115. #endif
  116.     return OK;
  117. }
  118.  
  119.  
  120. /****************************************************************************
  121. FUNCTION:
  122.     close receive data file
  123.  
  124. CALLING PARAMETERS:
  125.     pad:
  126.         sets pad for log file
  127. ===========================================================================*/
  128. closerx(pad)
  129. {
  130.         /* close only if sucessful open */
  131.     if(Rfile)
  132.     {
  133.             /* flush all buffers before closing */
  134.         fflush(fout);
  135.         fclose(fout);
  136.         Rfile=FALSE;
  137. #ifdef LOGRX
  138.         logfile(LOGRX, Rname, pad?'*':'R');    /* record file xmsn */
  139. #endif
  140.     }
  141. }
  142.  
  143.  
  144. /****************************************************************************
  145. FUNCTION:
  146.     get file size and return number bytes occupied by file
  147.  
  148. CALLING PARAMETERS:
  149.     *pathname:
  150.         pointer to file pathname.
  151. ===========================================================================*/
  152. long getfilesize(pathname)
  153. char *pathname;
  154. {
  155.     struct find_buf f_buf;
  156.     union REGS inregs;
  157.     union REGS outregs;
  158.  
  159.         /* DMA must point to f_buf structure */
  160.     bdos(SETDMA, &f_buf);
  161.         /* set pointer to file name */
  162.     inregs.x.dx = (short)pathname;
  163.         /* find first DOS function */
  164.     inregs.h.ah = FINDFIRST;
  165.         /* allow search for files only */
  166.     inregs.x.cx = DIRAT;
  167.  
  168.         /* dos request function */
  169.     intdos(&inregs,&outregs);
  170.     if (outregs.h.al != NULL)
  171.         return NULL;
  172.  
  173.         /* return number of blocks used by file */
  174.     return f_buf.filesize;
  175. } /* getfilesize */
  176.  
  177.  
  178. /****************************************************************************
  179. FUNCTION:
  180.     open a file for transmission to a remote
  181.  
  182. CALLING PARAMETERS:
  183.     mode:
  184.         0=don't compute file size
  185.         1=compute file size
  186.         >1=use file size already computed in f_buf
  187.     f_buf:
  188.         pointer to a structure containing information about file. if
  189.         0 routine will not report file transmission time.  If 1, file
  190.         size must be calculated, otherwise the structure is already
  191.         initialized.
  192.     *pathname:
  193.         pointer to a string representing file name to be opened
  194. ===========================================================================*/
  195. opentx(mode,f_buf,pathname)
  196. struct find_buf *f_buf;
  197. char *pathname;
  198. int mode;
  199. {
  200.     int blocks;
  201.     long bytes;
  202.  
  203.         /* set file name into global buffer */
  204.     unspace(pathname);
  205.     strcpy(Tname, pathname);
  206.  
  207.     printf("'%s' ", pathname);
  208.     if((fin=fopen(pathname,"rb"))==NULL)
  209.     {
  210.         printf("Can't open\n");
  211.         return ERROR;
  212.     }
  213.     printf("Open\n");
  214.  
  215. #ifdef RESTRICTED
  216.             /* restriced file stuff needs to be added */
  217.         fclose(fin);
  218.         printf("Not for Distribution\n");
  219.         return ERROR;
  220. #endif
  221.  
  222.         /* transmit file is open */
  223.     Tfile= TRUE;
  224.  
  225.         /* compute file size. mode will <> NULL when f_buf it is initialized
  226.            by another routine */
  227.     if (mode == 1)
  228.         bytes = getfilesize(pathname);
  229.     else if (mode != NULL)
  230.         bytes = f_buf->filesize;
  231.  
  232.         /* display download time */
  233.     blocks = (int)(bytes/blklen + (bytes%blklen?1:0));
  234.     if (mode != NULL)
  235.         dis_xmit_time(1,blocks);
  236.  
  237.     return OK;
  238. } /* opentx */
  239.  
  240.  
  241. /****************************************************************************
  242. FUNCTION:
  243.     display transmission time of a file
  244.  
  245. CALLING PARAMETERS:
  246.     nfiles:
  247.         number of file(s) to be sent
  248.     blks:
  249.         number of blks occupied by file(s)
  250. ===========================================================================*/
  251. dis_xmit_time(nfiles,blks)
  252. unsigned nfiles;
  253. unsigned blks;
  254. {
  255.     unsigned spm;
  256.     unsigned dminutes;
  257.  
  258.         /* sector per minute */
  259.         /* (Baudrate*60)/(10 bits each char * 136 chars) */
  260.     spm = Baudrate/23;
  261.  
  262.         /* download minutes calculation */
  263.     dminutes = nfiles+((10*(nfiles+blks))/spm)+(blks/20);
  264.  
  265.         /* display computed transmission time */
  266.         /* display number and size.  #k/s = 128b/s / 1024b/k = 1/8 */
  267.     printf("%u Blocks %dk %u.%u Minutes Xmsn Time at %u Baud\n",
  268.         blks, blks/8, dminutes/10, dminutes%10, Baudrate);
  269.  
  270.     /* display file name, length and minutes for transfer on status line */
  271. #ifdef STATLINE
  272.     lpstat("%s %u Blocks %dk %u.%u Min",
  273.         Tname, blks, blks/8, dminutes/10, dminutes%10);
  274. #endif
  275. } /* dis_xmit_time */
  276.  
  277.  
  278. /****************************************************************************
  279. FUNCTION:
  280.     closetx(status) call with status != 0 if incomplete file xmsn
  281.  
  282. CALLING PARAMETERS:
  283.     status:
  284.         if false and LOGTX, will call logfile with Tname
  285. ===========================================================================*/
  286. closetx(status)
  287. {
  288.     if(Tfile)
  289.     {
  290.         fclose(fin);
  291. #ifdef LOGTX
  292.         if(!status)
  293.             logfile(LOGTX, Tname, 's');    /* record file xmsn */
  294. #endif
  295.         Tfile=FALSE;
  296.     }
  297. } /* closetx */
  298.  
  299.  
  300. /****************************************************************************
  301. FUNCTION:
  302.     search the phone file for name
  303.  
  304. CALLING PARAMETERS:
  305.     name:
  306.         pointer to name to search for in phone file
  307.     buffer:
  308.         pointer to buffer to use to read phone file into
  309.  
  310. RETURNED PARAMETERS:
  311.     TRUE if succesful, ERROR if something went wrong
  312. ===========================================================================*/
  313. #ifdef PHONES
  314. getphone(name, buffer)
  315. char *name, *buffer;
  316. {
  317.     closetx(TRUE);
  318.  
  319.     searchpath(PHONES,Utility.ubuf);
  320.     if((fin=fopen(Utility.ubuf,"rb"))==NULL)
  321.     {
  322.         p